<template>
  <div :class="{ 'navbar-com': true, expand: menuStore.expandFold }">
    <div class="navbar-left">
      <div class="navbar-left-top">
        <div class="expand-fold" @click="changeExpand">
          <el-icon class="expand-fold-icon" v-if="menuStore.expandFold">
            <Expand />
          </el-icon>
          <el-icon class="expand-fold-icon" v-if="!menuStore.expandFold">
            <Fold />
          </el-icon>
        </div>
      </div>
      <div class="navbar-left-bottom">
        <div class="return-refresh-home">
          <div class="icon-item" @click="returnFun">
            <el-icon class="icon">
              <ArrowLeft />
            </el-icon>
          </div>
          <div class="icon-item" @click="refreshFun">
            <el-icon class="icon">
              <RefreshRight />
            </el-icon>
          </div>
          <div class="icon-item" @click="HomeFun">
            <el-icon class="icon">
              <House />
            </el-icon>
          </div>
        </div>
        <div class="tab-box">
          <div :class="{ 'tab-item': true, active: indexAct == item.index }" v-for="(item, index) in menuStore.tabs"
            :key="index" @click="goTab(item.index)">
            <div class="name">{{ item.name }}</div>
            <el-icon class="icon" @click="delTab(item.index)">
              <Close />
            </el-icon>
          </div>
        </div>
      </div>
    </div>
    <el-dropdown class="el-dropdown-box">
      <span class="el-dropdown-link" v-if="userStore.userInfo">
        {{ userStore.userInfo.username
        }}<el-icon class="el-icon--right">
          <arrow-down />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item :icon="Plus" @click="logout">退出</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>
<script setup>
import { computed } from 'vue'
import { removeCookie } from '@utils/jsCookie'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/pinia/user'
import { useMenuStore } from '@/pinia/menu'
import actions from '../../store/actions'

const userStore = useUserStore()
const menuStore = useMenuStore()

// const menu = JSON.parse(JSON.stringify(menuStore.menus))

const router = useRouter()
const route = useRoute()

let indexAct = computed(() => {
  let path = route.path.split('/')
  return `/${path[1]}/${path[2]}`
})

// 退出登录
function logout() {
  userStore.logout()
  router.push({ path: '/login' })
}

// 返回
const returnFun = () => {
  router.go(-1)
}

// 刷新
const refreshFun = () => {
  router.go(0)
}

// 主页
const HomeFun = () => {
  router.push({ path: '/home/index' })
}

// 展开折叠
const changeExpand = () => {
  menuStore.expandFoldAct()
}

// tab跳转
const goTab = (index) => {
  router.push(index)
}

// 删除标签
const delTab = (index) => {
  let tabs = JSON.parse(JSON.stringify(menuStore.tabs))
  let spliceIndex = 0
  tabs.forEach((v, i, arr) => {
    v.active = false
    if (v.index == index) {
      tabs.splice(i, 1)
      spliceIndex = i
    }
  })

  if (tabs[spliceIndex]) {
    tabs[spliceIndex].active = true
    menuStore.setTabs(tabs)
    setTimeout(function () {
      router.push(tabs[spliceIndex].index)
    }, 200)
  } else if (tabs[spliceIndex - 1]) {
    tabs[spliceIndex - 1].active = true
    menuStore.setTabs(tabs)
    setTimeout(function () {
      router.push(tabs[spliceIndex - 1].index)
    }, 200)
  }
}
</script>
<style lang="scss">
.navbar-com {
  position: fixed;
  left: 200px;
  top: 0;
  right: 0;
  padding-right: 15px;
  box-sizing: border-box;
  height: 60px;
  line-height: 60px;
  background-color: #fff;
  color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 5px 0 rgb(48 49 51 / 15%), 0 0 1px 0 rgb(48 49 51 / 24%);
  transition: all 0.5s;

  .navbar-left {
    flex-grow: 1;

    .navbar-left-top {
      height: 30px;
      line-height: 30px;
      padding-left: 15px;
      @include flex();

      .expand-fold {
        @include flex();
        height: 30px;
        cursor: pointer;

        .expand-fold-icon {
          color: #636564;
          font-size: 20px;
        }
      }
    }

    .navbar-left-bottom {
      width: 100%;
      height: 30px;
      line-height: 30px;
      @include flex();

      .return-refresh-home {
        @include flex();

        .icon-item {
          height: 30px;
          width: 30px;
          box-sizing: border-box;
          border-top: 1px solid #ededed;
          border-right: 1px solid #ededed;
          font-size: 16px;
          color: #9d9d9d;
          cursor: pointer;
          @include flex(row, center, center);
        }
      }

      .tab-box {
        height: 30px;
        padding-right: 15px;
        box-sizing: border-box;
        flex-grow: 1;
        @include flex();

        .tab-item {
          width: 90px;
          height: 30px;
          border-top: 2px solid #fff;
          color: #7f8180;
          font-size: 12px;
          box-sizing: border-box;
          @include flex(row, center, center);
          cursor: pointer;

          .name {
            width: 60px;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
          }

          .icon {
            color: #a7a7a7;
            margin-left: 5px;
          }
        }

        .tab-item.active {
          border-top: 2px solid #202221;
          color: #252726;
          background-color: #f5f5f5;
        }
      }
    }
  }
}

.navbar-com.expand {
  left: 63px;
}
</style>
<style>
.el-dropdown-box {
  cursor: pointer;
  height: 50px;
  line-height: 50px;
}
</style>
